package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.ReportService;
import com.sky.service.SetMealService;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Slf4j
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private WorkspaceMapper workspaceMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 工作台统计数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        log.info("begin:{},end:{}",begin,end);
        DateVO dateVO = DateVO.builder().begin(begin).end(end).build();
        //查询当天的新增用户数据!!!
        Integer ToDayNewUserCount = userMapper.countNewUser(dateVO);
        //当日订单总数查询
        BigDecimal ToDayOrderCount = new BigDecimal(orderMapper.getTurnoverReportList(dateVO));
        //查询当日完成的订单的数量!!!
        BigDecimal ToDayValidOrderCount = new BigDecimal(orderMapper.validOrderCountList(dateVO));
        BigDecimal orderCompletionRate = new BigDecimal(0);
        if(ToDayOrderCount.compareTo(BigDecimal.ZERO)!=0 || ToDayValidOrderCount.compareTo(BigDecimal.ZERO)!=0){
            orderCompletionRate = ToDayValidOrderCount.divide(ToDayOrderCount, 2, RoundingMode.HALF_UP);
        }
        //查询当天的营业额
        List<CountVO> turnoverReport = reportMapper.getTurnoverReport(dateVO);
        //拆解集合
        BigDecimal ToDayPrice = turnoverReport.isEmpty()?BigDecimal.ZERO:turnoverReport.get(0).getPrice();
        //获取平均客单价!!!
        BigDecimal unitPrice = ToDayValidOrderCount.compareTo(BigDecimal.ZERO)!=0?ToDayPrice.divide(ToDayValidOrderCount, 2, RoundingMode.HALF_UP):BigDecimal.ZERO;
        BusinessDataVO build = BusinessDataVO.builder()
                .newUsers(ToDayNewUserCount)
                .orderCompletionRate(orderCompletionRate)
                .turnover(ToDayPrice)
                .unitPrice(unitPrice)
                .validOrderCount(ToDayValidOrderCount.intValue())
                .build();
        log.info("控制台需要的今天的数据!!{}",build);
        return build;
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer disabled = setMealMapper.getSetmealStatusCountById(StatusConstant.DISABLE);
        Integer enabled = setMealMapper.getSetmealStatusCountById(StatusConstant.ENABLE);
        SetmealOverViewVO build = SetmealOverViewVO.builder()
                .sold(enabled)
                .discontinued(disabled)
                .build();
        return build;
    }

    /**
     * 菜品总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewDishes() {
        Integer disabled = dishMapper.getDishStatusCountById(StatusConstant.DISABLE);
        Integer enabled = dishMapper.getDishStatusCountById(StatusConstant.ENABLE);
        SetmealOverViewVO build = SetmealOverViewVO.builder()
                .sold(enabled)
                .discontinued(disabled)
                .build();
        return build;
    }

    /**
     * 订单总览
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        //查询所有订单
        Integer allOrders = orderMapper.countOrders();
        //根据不同状态查询订单
        //已取消数
        Integer cancelledOrders = orderMapper.getOrderStatusCount(Orders.CANCELLED);
        //已完成数
        Integer completedOrders = orderMapper.getOrderStatusCount(Orders.COMPLETED);
        //待派送
        Integer deliveredOrders = orderMapper.getOrderStatusCount(Orders.DELIVERY_IN_PROGRESS);
        //待接单
        Integer waitingOrders = orderMapper.getOrderStatusCount(Orders.TO_BE_CONFIRMED);
        OrderOverViewVO build = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
        log.info("订单总览:{}",build);
        return build;
    }
}
